package
{
	import com.freshcity.common.MainLoadBar;
	import com.freshcity.core.GlobalVar;
	import com.greensock.events.LoaderEvent;
	import com.greensock.loading.LoaderMax;
	import com.greensock.loading.SWFLoader;
	import com.greensock.loading.XMLLoader;
	import com.greensock.loading.display.ContentDisplay;
	
	import flash.display.GradientType;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;

	public class LoadBar extends Sprite
	{
		public var userData:Object;
		public var barBack:Shape;
		public var loadBar:Shape;
		public var loadTxt:TextField;
		private var queue:LoaderMax;

		public function LoadBar()
		{
			this.addEventListener(Event.ADDED_TO_STAGE, this.initApp);
		}

		protected function initApp(event:Event):void
		{
			var errorHandler:Function=function(e:Event):void
			{
			};
			//create a LoaderMax named "mainQueue" and set up onProgress, onComplete and onError listeners
			queue=new LoaderMax({name: "mainQueue", onProgress: onProgressHandler, onComplete: onCompleteHandler, onError: errorHandler});
			//append several loaders
			queue.append(new SWFLoader(GlobalVar.MAIN_PATH + "/mytest.swf?v=" + GlobalVar.version, {name: "mainClip", estimatedBytes: 2000, container: this}));
			queue.append(new SWFLoader(GlobalVar.MAIN_PATH + "/sounds.swf?v=" + GlobalVar.version, {name: "sound"}));
			queue.append(new XMLLoader(GlobalVar.MAIN_PATH + "/data.xml?v=" + GlobalVar.version, {name: "dataXML"}));
			queue.append(new XMLLoader(GlobalVar.MAIN_PATH + "/gamedef.xml?v=" + GlobalVar.version, {name: "configXML"}));
			//queue.append( new ImageLoader("img/photo1.jpg", {name:"photo1", estimatedBytes:2400, container:this, alpha:0, width:250, height:150, scaleMode:"proportionalInside"}) );
			//add a loader to the top of the queue using prepend()
			//queue.prepend( new MP3Loader("mp3/audio.mp3", {name:"audio", repeat:100, autoPlay:true}) );
			//prioritize the loader named "photo1"
			//LoaderMax.prioritize("photo1");  //same as LoaderMax.getLoader("photo1").prioritize();
			//start loading
			queue.load();
		}

		private function onProgressHandler(e:LoaderEvent):void
		{
			loadTxt.text="已载入"+int(e.target.progress*100)+" %";
			loadBar.scaleX=e.target.progress;
		}

		private function onCompleteHandler(e:Event):void
		{
			var image:ContentDisplay=LoaderMax.getContent("mainClip");
			if (this.userData.pass == false)
			{
				image.rawContent.loadRegister(this.userData);
			}
			else
			{
				image.rawContent.setUserData(this.userData);
			}
			
			loadBar.parent.removeChild(loadBar);
			loadTxt.parent.removeChild(loadTxt);
			barBack.parent.removeChild(barBack);
		}
	}
}
